function	script	StoneBoard	{

    function setMessage {
        do
        {
            clear;
            mes l("Please enter the message:");
            next;
            input .@msg$;
            .@msg$ = strip(.@msg$);
            if (.@msg$ != "")
            {
                return .@msg$;
            }
            mes l("The message cannot be empty");
            next;
        } while (1);
    }

    function setInterval {
        clear;
        mes l("Please select the interval:");
        next;
        menuint
            l("Every 1 hour"), 1,
            l("Every 3 hours"), 3,
            l("Every 5 hours"), 5,
            l("Every 6 hours"), 6,
            l("Every 12 hours"), 12,
            l("Every 24 hours"), 24,
            l("Never (only on login)"), 0;

        return @menuret;
    }

    function setMaxRep {
        if (getarg(0,0) == 0)
        {
            return 0;
        }
        clear;
        mes l("Repeat how many times?");
        next;
        menuint
            l("Send only once"), 1,
            l("Send 2 times"), 2,
            l("Send 3 times"), 3,
            l("Send 5 times"), 5,
            l("Send 10 times"), 10,
            l("Send 20 times"), 20,
            l("Send indefinitely"), 0;

        return @menuret;
    }

    function setOnLogin {
        if (getarg(0,0) == 0)
        {
            return 1;
        }
        clear;
        mes l("Send this message also on login?");
        next;
        select
            l("No"),
            l("Yes");

        return (@menu - 1);
    }

    function newBroadcast {
        do
        {
            setnpcdialogtitle l("Scheduled broadcasts - Create new");

            // go through all steps
            .@msg$ = setMessage();
            .@int = setInterval();
            .@max = setMaxRep(.@int);
            .@login = setOnLogin(.@int);

            // recap
            clear;
            mes l("Message:");
            mes "---";
            mes .@msg$;
            mes "---";
            if (.@int)
            {
                mes l("Interval: every @@ hour(s)", .@int);
                mes l("Repeat: @@ times", .@max ? .@max : "∞");
                mes l("Sent on login: @@", .@login ? l("yes") : l("no"));
            }
            else
            {
                mes l("Interval: (none, only sent on login)");
                mes l("Sent on login: yes");
            }

            next;
            select
                menuimage("actions/cancel", l("Discard")),
                menuimage("actions/edit", l("Start over")),
                menuimage("actions/test", l("Start broadcasting")),
                menuimage("actions/test", l("Start broadcasting, and make an extra broadcast right now"));

            switch (@menu)
            {
                case 3:
                case 4:
                        stopnpctimer "@sched";
                        $@SCHED_Opt[0] = .@login;
                        $@SCHED_Opt[1] = .@int;
                        $@SCHED_Opt[2] = 0;
                        $@SCHED_Opt[3] = .@max;
                        $@SCHED_Opt[4] = 0;
                        $@SCHED_Msg$ = .@msg$;
                        if (.@int)
                        {
                            initnpctimer "@sched";
                        }
                        if (@menu == 4)
                        {
                            announce $@SCHED_Msg$, bc_all;
                        }
                case 1: return;
            }

        } while(1);
    }

    do
    {
        clear;
        setnpcdialogtitle l("Scheduled broadcasts");
        mes l("This menu allows you to set the scheduled broadcast that is sent to all players at a specific interval.");
        mes "";

        .@a = $@SCHED_Msg$ != ""; // any active broadcast?
        mes "---";
        mes .@a ? $@SCHED_Msg$ : "(" + l("no active broadcast") +")";
        mes "---";
        if (.@a)
        {
            mes l("Sent on login: @@", ($@SCHED_Opt[0] ? l("yes") : l("no")));
            if ($@SCHED_Opt[1])
            {
                .@next = max(1, ((3600000 * ($@SCHED_Opt[1] - $@SCHED_Opt[4])) - getnpctimer(0, "@sched")));
                mes l("Interval: every @@ hour(s)", $@SCHED_Opt[1]);
                mes l("Next broadcast: @@", FuzzyTimeFromMs(.@next));
            }
            else
            {
                mes l("Interval: (none, only sent on login)");
                mes l("Next broadcast: (never)");
            }
            mes l("Sent: @@ times out of @@", $@SCHED_Opt[2], ($@SCHED_Opt[3] ? $@SCHED_Opt[3] : "∞"));
        }
        next;

        select
            menuimage("actions/abort", l("Abort")),
            rif(.@a, menuimage("actions/test", l("Manually trigger the current broadcast"))),
            rif(.@a, menuimage("actions/remove", l("Stop broadcasting"))),
            rif(!(.@a), menuimage("actions/add", l("Set a new broadcast"))),
            rif(getarg(0,0), menuimage("actions/home", l("Return to Super Menu")));

        switch (@menu)
        {
            case 2: announce $@SCHED_Msg$, bc_all; break;
            case 3: $@SCHED_Msg$ = ""; break;
            case 4: newBroadcast; break;
            default: return;
        }
    } while (1);
}



-	script	@sched	32767,{
    end;

OnTimer3600000:
    if ($@SCHED_Msg$ == "")
    {
        stopnpctimer;
        end;
    }

    ++$@SCHED_Opt[4]; // increase hours counter
    if ($@SCHED_Opt[4] == $@SCHED_Opt[1])
    {
        stopnpctimer;
        ++$@SCHED_Opt[2]; // increase total counter
        announce $@SCHED_Msg$, bc_all;
        $@SCHED_Opt[4] = 0; // reset hours counter
        if ($@SCHED_Opt[2] >= $@SCHED_Opt[3] && $@SCHED_Opt[3] > 0)
        {
            $@SCHED_Msg$ = ""; // reset message
            end;
        }
    }
    initnpctimer;
    end;

OnCall:
    StoneBoard;
    closedialog;
    end;

OnPCLoginEvent:
    if ($@SCHED_Opt[0] && $@SCHED_Msg$ != "")
    {
        announce $@SCHED_Msg$, bc_self;
    }
    end;

OnInit:
    bindatcmd "sched", "@sched::OnCall", 3, 99, 0;
}
